package com.zh.flowable.support;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * @author
 * @date 2025/8/22
 */
public final class LoginUserHolder {
    public static final String SYSTEM_USERNAME = "SYSTEM";

    private static final ThreadLocal<String[]> userInfo = new ThreadLocal<>();
    private static final ThreadLocal<String> userType = new ThreadLocal<>();
    private static final ThreadLocal<Map<String, String>> attrs = ThreadLocal.withInitial(HashMap::new);

    private LoginUserHolder() {
    }

    /**
     * 暂存当前登录用户信息
     *
     * @param userId   用户ID
     * @param username 用户登录账号
     * @param roles    用户的角色的字符串拼接','
     */
    public static void putUser(@NotNull String userId, @NotNull String username, @NotNull String roles) {
        String[] objects = userInfo.get();
        if (objects == null) {
            userInfo.set(new String[]{userId, username, roles, ""});
        } else {
            objects[0] = userId;
            objects[1] = username;
            objects[2] = roles;
        }
    }

    public static void putTenantId(@NotNull String tenantId) {
        String[] objects = userInfo.get();
        if (objects == null) {
            userInfo.set(new String[]{"", "", "", tenantId});
        } else {
            objects[3] = tenantId;
        }
    }

    /**
     * 获取当前用户的 tenantId
     *
     * @return tenantId 若没有则返回空字符串
     */
    @NotNull
    public static String tenantId() {
        String[] objects = userInfo.get();
        if (objects != null) {
            return objects[3];
        } else {
            return "";
        }
    }

    public static void putUserType(@NotNull String type) {
        userType.set(type);
    }

    /**
     * 获取当前用户的 userType
     *
     * @return userType 若没有则返回空字符串
     */
    @NotNull
    public static String userType() {
        return Optional.ofNullable(userType.get()).orElse("");
    }

    /**
     * 获取当前用户的 userId
     *
     * @return userId 若没有则返回空字符串
     */
    @NotNull
    public static String currentUserId() {
        String[] objects = userInfo.get();
        if (objects != null) {
            return objects[0];
        } else {
            return "";
        }
    }

    /**
     * 获取当前用户的 username
     *
     * @return username 若没有则返回空字符串
     */
    @NotNull
    public static String currentUsername() {
        String[] objects = userInfo.get();
        if (objects != null) {
            return objects[1];
        } else {
            return "";
        }
    }

    @NotNull
    public static List<String> currentUserRoles() {
        String[] objects = userInfo.get();
        if (objects != null) {
            String roles = objects[2];
            if (!StringUtils.hasText(roles)) {
                return List.of();
            }
            return List.of(roles.split(","));
        } else {
            return List.of();
        }
    }

    public static void add(@NotNull String key, @NotNull String value) {
        attrs.get().putIfAbsent(key, value);
    }

    @Nullable
    public static String get(@NotNull String key) {
        return attrs.get().get(key);
    }

    public static void remove() {
        userInfo.remove();
        userType.remove();
        attrs.remove();
    }

}

